﻿

CREATE PROCEDURE [dbo].[resuma_StergeUnitate] 
 @CodUnitate smallint AS
BEGIN
DECLARE @NrDoc AS int
SET @NrDoc = 0

BEGIN TRAN

PRINT '[ContracteMunca]'
DELETE FROM [ContracteMunca] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare 
    
PRINT '[AngajatiAdaosuri]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajatiAdaosuri]') AND type in (N'U'))
	DELETE FROM [AngajatiAdaosuri] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 
PRINT '[AngajatiOcupatii]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajatiOcupatii]') AND type in (N'U'))
	DELETE FROM [AngajatiOcupatii] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 
PRINT '[AngajatiPontaj]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajatiPontaj]') AND type in (N'U'))
	DELETE FROM [AngajatiPontaj] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[AngajatiSporuri]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajatiSporuri]') AND type in (N'U'))
	DELETE FROM [AngajatiSporuri] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[AngajatiSalarizare]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajatiSalarizare]') AND type in (N'U'))
	DELETE FROM [AngajatiSalarizare] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[ConcediiMedicale]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ConcediiMedicale]') AND type in (N'U'))
	DELETE FROM [ConcediiMedicale] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
   GOTO Eroare 

PRINT '[ContracteMunca]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ContracteMunca]') AND type in (N'U'))
	DELETE FROM [ContracteMunca] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 
    
PRINT '[PersoaneSuspCM]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].PersoaneSuspCM') AND type in (N'U'))
	DELETE FROM PersoaneSuspCM WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare     

PRINT '[SituatieMilitara]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SituatieMilitara]') AND type in (N'U'))
	DELETE FROM [SituatieMilitara] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare  
    
PRINT '[SpecialitatiPersoane]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SpecialitatiPersoane]') AND type in (N'U'))
	DELETE FROM [SpecialitatiPersoane] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[CalculSporuri]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CalculSporuri]') AND type in (N'U'))
	DELETE FROM [CalculSporuri] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[RaportareFoiBoala]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RaportareFoiBoala]') AND type in (N'U'))
	DELETE FROM [RaportareFoiBoala] WHERE [IDPersoana] IN (SELECT IDPersoana FROM [Persoane] WHERE [CodUnitate] = @CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 



PRINT '[Persoane]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Persoane]') AND type in (N'U'))
	DELETE FROM [Persoane] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[PosturiVacante]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PosturiVacante]') AND type in (N'U'))
	DELETE FROM [PosturiVacante] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[ResumaSetup]'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ResUmaSetup]') AND type in (N'U'))
	DELETE FROM [ResUmaSetup] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 



COMMIT TRAN    
GOTO Gata
Eroare:
  ROLLBACK
  RAISERROR ('Eroare la executia procedurii StergeUnitate',-1,0)
Exista_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine documente!',-1,0)
Exista_Rulaje_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine rulaje - documente!',-1,0)

Gata:

END